package com.itrip.repository.Impl;

import com.itrip.document.Hotel;
import com.itrip.repository.HotelRepository;
import com.itrip.vo.hotel.HotelVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Repository
public class HotelRepositoryImpl implements HotelRepository {
    @Resource
    private SolrClient solrClient;

    @Override
    public long getHotelCount(HotelVO hotelVO) throws IOException, SolrServerException {
        SolrQuery solrQuery = new SolrQuery();
        /**
         * 关键字查询
         */
        if (!StringUtils.isEmpty(hotelVO.getKeyword())) {
            solrQuery.setQuery("hotelName:"+hotelVO.getKeyword()+" or address:"+hotelVO.getKeyword());
        } else {
            solrQuery.setQuery("*:*");
        }


        String grade = hotelVO.getHotelLevel();
        String toponym = hotelVO.getRedundantCityName();

        /**
         * 精确查询
         */
        if (!StringUtils.isEmpty(grade)  && !StringUtils.isEmpty(toponym)){
            solrQuery.addFilterQuery("hotelLevel:" +grade);
            solrQuery.addFilterQuery("redundantCityName:" +toponym);
        } else if (!StringUtils.isEmpty(grade) & StringUtils.isEmpty(toponym)){
            solrQuery.addFilterQuery("hotelLevel:" +grade);
        } else if (StringUtils.isEmpty(grade) & !StringUtils.isEmpty(toponym)){
            solrQuery.addFilterQuery("redundantCityName:" +toponym);
        }

        //查询数据
        QueryResponse queryResponse = solrClient.query("hotel", solrQuery);

        //获取数据行数
        return queryResponse.getResults().getNumFound();

    }

    @Override
    public List<Hotel> getHotelByQuery(HotelVO hotelVO, Integer start, Integer pageSize) throws IOException, SolrServerException {

        //查询条件
        SolrQuery solrQuery = new SolrQuery();


        if(!StringUtils.isEmpty(hotelVO.getKeyword())){
            solrQuery.setQuery("hotelName:"+hotelVO.getKeyword()+" or address:"+hotelVO.getKeyword());
        }else{
            solrQuery.setQuery("*:*");
        }

        /**
         * 精准查询
         * 从前端拿数据
         */
        String grade = hotelVO.getHotelLevel();
        String toponym = hotelVO.getRedundantCityName();

        if (!StringUtils.isEmpty(grade)  && !StringUtils.isEmpty(toponym)){
            solrQuery.addFilterQuery("hotelLevel:" +grade);
            solrQuery.addFilterQuery("redundantCityName:" +toponym);
        } else if (!StringUtils.isEmpty(grade) & StringUtils.isEmpty(toponym)){
            solrQuery.addFilterQuery("hotelLevel:" +grade);
        } else if (StringUtils.isEmpty(grade) & !StringUtils.isEmpty(toponym)){
            solrQuery.addFilterQuery("redundantCityName:" +toponym);
        }

        solrQuery.setStart(start);

        solrQuery.setRows(pageSize);
        //高亮设置
        solrQuery.setHighlight(true);//启用高亮查询
        solrQuery.addHighlightField("hotelName");//添加高亮的字段
        solrQuery.addHighlightField("address");//添加高亮的字段
        solrQuery.setHighlightSimplePre("<font color='red'>");//高亮前缀
        solrQuery.setHighlightSimplePost("</font>");//高亮后缀

        //查询数据
        QueryResponse queryResponse = solrClient.query("hotel", solrQuery);

        //获取普通结果
        List<Hotel> hotelList = queryResponse.getBeans(Hotel.class);
        //获取高亮结果
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();

        //替换普通结果中的字段 为 高亮结果中的值
        for (Hotel hotel:hotelList) {
            //判断高亮结果是否为空
            List<String> hotelName = highlighting.get(hotel.getId()).get("hotelName");
            if(hotelName!=null && hotelName.size()>0){
                hotel.setHotelName(hotelName.get(0));
            }

            List<String> address = highlighting.get(hotel.getId()).get("address");
            if(address!=null && address.size()>0){
                hotel.setAddress(address.get(0));
            }
        }

        return hotelList;
    }


}
